﻿Imports Telerik.Web.UI

Partial Class Setup_ItemUnit
    Inherits System.Web.UI.Page

    Dim ItemUnitObj As New DBItemUnitSP
    Dim ItemUnitDataSet As New Data.DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")

            CType(WUCItem.FindControl("rcbv"), RadComboBox).AutoPostBack = True

        End If
    End Sub

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub WUCItem_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
        If Not (WUCItem.SelectedValue = "") Then

            Dim ItemNo As String = WUCItem.SelectedValue.ToString()
            rgUnit.DataBind()
            LoadSelection(ItemNo)
        Else
            rgUnit.DataBind()
        End If
    End Sub


    Sub LoadSelection(ByVal No As String)

        ItemUnitDataSet = ItemUnitObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvItemUnitByItemID", No)

        If Not ItemUnitDataSet Is Nothing Then
            If ItemUnitDataSet.Tables.Count > 0 Then
                If ItemUnitDataSet.Tables(0).Rows.Count > 0 Then

                    For Each item As GridDataItem In rgUnit.MasterTableView.Items

                        Dim id As String = item.GetDataKeyValue("UnitID").ToString()

                        For Each dr As Data.DataRow In ItemUnitDataSet.Tables(0).Rows
                            If dr.Item("UnitID").ToString() = id Then
                                item.Selected = True
                            Else
                                'item.Selected = False
                            End If
                        Next

                    Next

                End If
            End If
        End If

    End Sub


    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        If rgUnit.SelectedItems.Count > 0 Then
            Dim No As String = WUCItem.SelectedValue.ToString()
            ItemUnitObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeleteItemUnit", No)

            For Each item As GridDataItem In rgUnit.MasterTableView.Items
                If item.Selected = True Then
                    Dim id As String = item.GetDataKeyValue("UnitID").ToString()
                    ItemUnitObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "SaveItemUnit", No,id)
                End If
            Next
            'RadWindowManager1.IconUrl = "~/Design/images/success.png"
            RadAjaxManager1.ResponseScripts.Add("radalert('Successful !', 150, 100);")
        Else
            'RadWindowManager1.IconUrl.Remove(0)
            RadAjaxManager1.ResponseScripts.Add("radalert('Select at Least one Unit !', 350, 100);")
        End If
    End Sub


End Class
